package org.dromara.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import org.dromara.common.core.params.AppTypeName;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.app.domain.bo.SkYueJuTypeBo;
import org.dromara.app.domain.vo.SkYueJuTypeVo;
import org.dromara.app.domain.SkYueJuType;
import org.dromara.app.mapper.SkYueJuTypeMapper;
import org.dromara.app.service.ISkYueJuTypeService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 约局类型Service业务层处理
 *
 * @author Evacuation
 * @date 2024-11-07
 */
@RequiredArgsConstructor
@Service
public class SkYueJuTypeServiceImpl implements ISkYueJuTypeService {

    private final SkYueJuTypeMapper baseMapper;

    /**
     * 查询约局类型
     *
     * @param id 主键
     * @return 约局类型
     */
    @Override
    public SkYueJuTypeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询约局类型列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 约局类型分页列表
     */
    @Override
    public TableDataInfo<SkYueJuTypeVo> queryPageList(SkYueJuTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SkYueJuType> lqw = buildQueryWrapper(bo);
        Page<SkYueJuTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        if (result.getRecords().size()>0){
            for(SkYueJuTypeVo vo:result.getRecords()){
                List<SkYueJuTypeVo> list = baseMapper.selectVoList(
                    new QueryWrapper<SkYueJuType>().lambda()
                        .eq(SkYueJuType::getParentId,vo.getId())
                );
                vo.setChildren(list);
            }
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的约局类型列表
     *
     * @param bo 查询条件
     * @return 约局类型列表
     */
    @Override
    public List<SkYueJuTypeVo> queryList(SkYueJuTypeBo bo) {
        LambdaQueryWrapper<SkYueJuType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SkYueJuType> buildQueryWrapper(SkYueJuTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SkYueJuType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SkYueJuType::getName, bo.getName());
        lqw.eq(bo.getParentId() != null, SkYueJuType::getParentId, bo.getParentId());
        lqw.eq(SkYueJuType::getParentId,0);
        return lqw;
    }

    /**
     * 新增约局类型
     *
     * @param bo 约局类型
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SkYueJuTypeBo bo) {
        SkYueJuType add = MapstructUtils.convert(bo, SkYueJuType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改约局类型
     *
     * @param bo 约局类型
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SkYueJuTypeBo bo) {
        SkYueJuType update = MapstructUtils.convert(bo, SkYueJuType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SkYueJuType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除约局类型信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
    /**
     * 派对大厅类型列表
     *
     * @param req
     * @return
     */
    @Override
    public List<AppTypeName> getList(HttpServletRequest req) {
        List<AppTypeName> list = baseMapper.getList(1L);
        return list;
    }
    /**
     * 约局类目列表
     *
     * @param req
     * @return
     */
    @Override
    public List<AppTypeName> getAllList(HttpServletRequest req) {
        List<AppTypeName> list = baseMapper.getList(0L);
        if (list.size()>0){
            for(AppTypeName app:list){
                app.setList(baseMapper.getList(app.getId()));
            }
        }
        return list;
    }

    @Override
    public List<SkYueJuTypeVo> allOneList() {
        List<SkYueJuTypeVo> list = baseMapper.selectVoList(
            new QueryWrapper<SkYueJuType>().lambda()
                .eq(SkYueJuType::getParentId,0)
        );
        return list;
    }
}
